const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");
// 保存送货路线区域
router.post('/deliver/grpOrders', async ctx => {
    let ids  = ctx.request.body.ids;
    let tenantId = ctx.user.tenantId
    ids = ids.join(',');
    let sql = `select invName,spec,unit1,unit2,case when mqty=0 then concat(sqty,unit2) when sqty=0 then concat(mqty,unit1) else concat(mqty,unit1,sqty,unit2) end assQty from
    (
        SELECT invName,spec,unit1,unit2,cast(qty/spec as int) mqty,cast(qty%spec as int) sqty from
        (
            SELECT g.name invName,g.spec,g.unit1,unit2,sum(ii.qty*ii.calNum ) qty from 
           invoice iv inner join  invoice_info ii on iv.tenantId=ii.tenantId and iv.id=ii.iid and iv.tenantId=${tenantId}
                      inner join goods g on ii.tenantId=g.tenantId and ii.invId=g.id   and iv.id in (${ids}) 
            where iv.transType in(150601,150603) and ii.trans<>N'换入' GROUP BY g.name,g.spec,g.unit1,g.unit2
        ) a
    ) a`
    let ret = await db.query(sql, {type: QueryTypes.SELECT});

    return ctx.suc('', ret);
})




module.exports = router;
